<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>index_life_cycle</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.js"></script>
</head>

<body>
  <div id="root">

  </div>
  <script>
    // 写一个生命周期
    console.log(Vue.config.optionMergeStrategies)

    const originalOptionMergeStrategies = Vue.config.optionMergeStrategies

    originalOptionMergeStrategies.exit = originalOptionMergeStrategies.created

    const notify = (name, vm) => {
      console.log('vm',vm)
      const lifeCycles = vm.$options[name]
      if (lifeCycles && lifeCycles.length > 0) {
        lifeCycles.forEach(lc => lc.call(vm))
      }
      const childs = vm.$children
      if (childs && childs.length > 0) {
        childs.forEach(v => notify(name, v))
      }
    }

    function bind(vm) {
      window.addEventListener('visibilitychange', ()=>{
        if (document.visibilityState === 'hidden') {
          notify('exit', vm)
        }
      })
    }
    const vm = new Vue({
      el: '#root',
      data: {
        msg: 'message--1111'
      },
      exit() {
        alert('我切换页面了')
      },
      template: `
        <div>hello {{ msg}}</div>
      `,
    })
    bind(vm)
  </script>
</body>

</html>